<?php
namespace app\admin\controller;


use think\facade\View;
use think\Model;
use think\Request;

class Merchant extends Base
{
    protected $ModelPath = 'Merchant';
    protected $model = null;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->con = "Merchant";
        $this->title = "企业";

        $module = $this->ModelPath;
        $this->model = new \app\admin\model\Merchant();

        View::assign(array(
            'module' => $module,
            'title' => $this->title,
            'con' => $this->con,
            'pageSize' => $this->pageSize
        ));
    }

    public function index()
    {
        if(request()->isPost()){
            $params = request()->param();
            $map = [];

//            $map[] = ['BenefitOrder.status', '=', 'payed'];

            if(!empty($params['time'])){
                $timeArr = explode(' - ', $params['time']);
                $map[] = ['BenefitOrder.create_time', '>=', $timeArr[0]];
                $map[] = ['BenefitOrder.create_time', '<=', $timeArr[1]];
            }
            if(!empty($params['user_id'])){
                $map[] = ['user_id', '=', $params['user_id']];
            }
            if(!empty($params['pay_amount'])){
                $map[] = ['pay_amount', '>=', $params['pay_amount']];
            }

            if(!empty($params['type'])){
                $curModel = \app\admin\model\BenefitOrder::where("status", 'payed')
                    ->group('user_id')->order('create_time ASC')->column('id', 'id');
                $curArr = implode(',', $curModel);
                if($params['type'] == 1){
                    //新增
//                    $this->model = $this->model->whereIn('id', $curArr);
//                    $totalAmount = \app\admin\model\BenefitOrder::where("status", 'payed')
//                        ->where($map)
//                        ->whereIn('id', $curArr)->sum('pay_amount');
                    $map[] = ['BenefitOrder.id', 'in', $curArr];
                }else{
                    //复投
                    $map[] = ['BenefitOrder.id', 'not in', $curArr];
                }
            }

            $total = $this->model
                ->where($map)
                ->count();

            $pageIndex = max(input('page'), 1);
            $pageSize = $this->pageSize;
            $p = ($pageIndex - 1) * $pageSize;

            $list = $this->model
                ->where($map)
                ->append(['pay_type_format', 'name_format', 'phone_format'])
                ->limit($p, $pageSize)
                ->select();

            $arr['data'] = $list;
            $arr['code'] = 0;
            // var_dump(1);die;
            $arr['count'] = $total;
            $arr['limit'] = $pageSize;
            $arr['msg'] = "获取" . $this->title . "列表成功";
            return json($arr);
        }else{
            return View::fetch();
        }
    }

    public function add(){
        if(request()->isPost()){
            $user_id = request()->param('user_id', 0);
            $arr = [];
            if($user_info = \app\admin\model\User::find($user_id)){
                $this->model->create([
                    'user_id'=>$user_id
                ]);
                $arr['status'] = 1;
                $arr['msg'] = '更新成功';
            }else{
                $arr['status'] = 0;
                $arr['msg'] = '查无此用户';
            }
            return $arr;
        }else{
            return View::fetch();
        }
    }

    public function edit(){
        if(request()->isPost()){
            $params = request()->param();
            $user_id = request()->param('user_id', 0);
            $arr = [];
            if($user_info = \app\admin\model\User::find($user_id)){
                $this->model->update($params);
                $arr['status'] = 1;
                $arr['msg'] = '添加成功';
            }else{
                $arr['status'] = 0;
                $arr['msg'] = '查无此用户';
            }
            return $arr;
        }else{
            $id = input('id');
            $info = $this->model->where('id', $id)->find();
            View::assign(array(
                'info'        =>    $info,
            ));
            return View::fetch();
        }
    }
}